#include "new_timer.h"

static timer_cb_t timer_cb[4] = {NULL, NULL, NULL, NULL};
static void *timer_cb_arg[4] = {NULL, NULL, NULL, NULL};

// void TMR_PWMInit(TMRX_t timer, PWMX_PolarTypeDef pr, PWM_RepeatTsTypeDef ts)
// {
//     switch (timer)
//     {
//     case TMR0:
//         TMR0_PWMInit(pr, ts);
//         break;
//     case TMR1:
//         TMR1_PWMInit(pr, ts);
//         break;
//     case TMR2:
//         TMR2_PWMInit(pr, ts);
//         break;
//     case TMR3:
//         TMR3_PWMInit(pr, ts);
//         break;
//     default:
//         break;
//     }
// }

static void timer_it_enable (timer_ch_t timer) {
    switch (timer) {
#if TIMER0_COMPILE_EN
    case TIMER0:
        timer_it_mode (TIMER0, ENABLE, TMR0_3_IT_CYC_END);  // 开启中断
        PFIC_EnableIRQ (TMR0_IRQn);
        break;
#endif
#if TIMER1_COMPILE_EN
    case TIMER1:
        timer_it_mode (TIMER1, ENABLE, TMR0_3_IT_CYC_END);  // 开启中断
        PFIC_EnableIRQ (TMR1_IRQn);
        break;
#endif
#if TIMER2_COMPILE_EN
    case TIMER2:
        timer_it_mode (TIMER2, ENABLE, TMR0_3_IT_CYC_END);  // 开启中断
        PFIC_EnableIRQ (TMR2_IRQn);
        break;
#endif
#if TIMER3_COMPILE_EN
    case TIMER3:
        timer_it_mode (TIMER3, ENABLE, TMR0_3_IT_CYC_END);  // 开启中断
        PFIC_EnableIRQ (TMR3_IRQn);
        break;
#endif
    default:
        break;
    }
}

void timer_it_disable (timer_ch_t timer) {
    switch (timer) {
#if TIMER0_COMPILE_EN
    case TIMER0:
        PFIC_DisableIRQ (TMR0_IRQn);
        break;
#endif
#if TIMER1_COMPILE_EN
    case TIMER1:
        PFIC_DisableIRQ (TMR1_IRQn);
        break;
#endif
#if TIMER2_COMPILE_EN
    case TIMER2:
        PFIC_DisableIRQ (TMR2_IRQn);
        break;
#endif
#if TIMER3_COMPILE_EN
    case TIMER3:
        PFIC_DisableIRQ (TMR3_IRQn);
        break;
#endif
    }
}

void timer_init (timer_ch_t timer, uint32_t tic_ms, timer_cb_t cb, void *arg) {
    uint8_t timer_id;
    size_t tic = GetSysClock() / 1000 * tic_ms;
    switch (timer) {
#if TIMER0_COMPILE_EN
    case TIMER0:
        TMR0_TimerInit (tic);
        timer_id = 0;
        break;
#endif
#if TIMER1_COMPILE_EN
    case TIMER1:
        TMR1_TimerInit (tic);
        timer_id = 1;
        break;
#endif
#if TIMER2_COMPILE_EN
    case TIMER2:
        TMR2_TimerInit (tic);
        timer_id = 2;
        break;
#endif
#if TIMER3_COMPILE_EN
    case TIMER3:
        TMR3_TimerInit (tic);
        timer_id = 3;
        break;
#endif
    default:
        break;
    }
    timer_cb[timer_id] = cb;
    timer_cb_arg[timer_id] = arg;
    timer_it_enable (timer);
}

#if TIMER0_COMPILE_EN
__INTERRUPT

__HIGH_CODE
void TMR0_IRQHandler (void)                // TMR0 定时中断
{
    TMR0_ClearITFlag (TMR0_3_IT_CYC_END);  // 清除中断标志
    if (timer_cb[0])
        timer_cb[0](timer_cb_arg[0]);
}
#endif

#if TIMER1_COMPILE_EN
__INTERRUPT

__HIGH_CODE
void TMR1_IRQHandler (void)                // TMR0 定时中断
{
    TMR1_ClearITFlag (TMR0_3_IT_CYC_END);  // 清除中断标志
    if (timer_cb[1])
        timer_cb[1](timer_cb_arg[1]);
}
#endif

#if TIMER2_COMPILE_EN
__INTERRUPT

__HIGH_CODE
void TMR2_IRQHandler (void)                // TMR0 定时中断
{
    TMR2_ClearITFlag (TMR0_3_IT_CYC_END);  // 清除中断标志
    if (timer_cb[2])
        timer_cb[2](timer_cb_arg[2]);
}
#endif

#if TIMER3_COMPILE_EN
__INTERRUPT

__HIGH_CODE
void TMR3_IRQHandler (void)                // TMR0 定时中断
{
    TMR3_ClearITFlag (TMR0_3_IT_CYC_END);  // 清除中断标志
    if (timer_cb[3])
        timer_cb[3](timer_cb_arg[3]);
}
#endif
